package com.yc.bran.snack.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.bran.snack.bean.Comment;
import com.yc.bran.snack.bean.GoodsInfo;
import com.yc.bran.snack.bean.MemberInfo;
import com.yc.bran.snack.dto.MemberInfoDto;
import com.yc.bran.snack.enums.ResultEnmu;
import com.yc.bran.snack.service.IMemberInfoService;
import com.yc.bran.snack.service.impl.CommentServiceImpl;
import com.yc.bran.snack.vo.ResultVo;

@RestController
@RequestMapping("/member")
public class MemberInfoController {
	@Autowired
	private IMemberInfoService memberInfoService;

	@Autowired  
	private StringRedisTemplate stringRedisTemplate;
	
	// 登陆
	@PostMapping("/login")
	public ResultVo login(MemberInfo mf) {
	    MemberInfo member = memberInfoService.login(mf);  
	    if (member == null) {  
	        return new ResultVo(ResultEnmu.ERROR);  
	    }  
	  
	    Integer uid = member.getUid();  
	    if (uid == null) {   
	        return new ResultVo(ResultEnmu.ERROR);
	    }  
	  
	    String avatarKey = "user:avatarUrl:" + uid; 
	    String upic = mf.getUpic();
	    if (upic != null) {  
	        stringRedisTemplate.opsForValue().set(avatarKey, upic);  
	    } else {  
	        System.out.println("用户头像未设置");  
	    }  
	    return new ResultVo(ResultEnmu.SUCCESS, member);  
	}


	@PostMapping("/check")
	public ResultVo check(MemberInfo mf) {
		MemberInfo member = memberInfoService.login(mf);
		return new ResultVo(ResultEnmu.SUCCESS, member);
	}

	@PostMapping("/loginout")
	public ResultVo loginout(MemberInfo mf) {
		MemberInfo member = memberInfoService.login(mf);
		return new ResultVo(ResultEnmu.SUCCESS, member);
	}

	//	上传头像
	@PostMapping("/upload")  
	public ResultVo updateProfilePic(@RequestParam("file") MultipartFile file, @RequestParam Integer uid) {  
		try {  
			if (file.isEmpty()) {  
				return new ResultVo(ResultEnmu.CHECK_ERROR, "上传的文件为空！"); 
			}  

			String upic = "http://127.0.0.1:8888/snackpics/" + memberInfoService.saveProfilePic(file, uid);
			if (upic != null && !upic.isEmpty()) { 
				memberInfoService.updateProfilePic(uid, upic);
				 String key = "user:avatarUrl:" + uid;  
			     stringRedisTemplate.opsForValue().set(key, upic);
				return new ResultVo(ResultEnmu.SUCCESS, upic);
				
			} else {  
				return new ResultVo(ResultEnmu.ERROR, "头像上传失败");  
			}  
		} catch (Exception e) {  
			return new ResultVo(ResultEnmu.ERROR, "服务器内部错误！"); 
		}  
	}  

	// 注册
	@PostMapping("/reg")  
	public ResultVo reg(@RequestBody MemberInfo mf) {  
		String key = "phone:" + mf.getUtel();  
		String code = stringRedisTemplate.opsForValue().get(key);  
		if (code == null) {  
			// 验证码不存在，可能是过期或被删除  
			return new ResultVo(ResultEnmu.CODE_TIMEOUT);  
		}  

		// 验证验证码是否正确  
		if (!code.equals(mf.getCode())) {  
			// 验证码错误  
			return new ResultVo(ResultEnmu.CODE_ERROR);  
		}  

		// 验证码正确
		int result = memberInfoService.reg(mf);  
		if (result > 0) {  
			// 注册成功删除Redis中的验证码
			return new ResultVo(ResultEnmu.SUCCESS);  
		} else {  
			// 注册失败  
			return new ResultVo(ResultEnmu.CHECK_ERROR);  
		}  
	}

	// 查询用户
	@PostMapping("/list")  
	public ResultVo list() {
		List<MemberInfo> list = memberInfoService.list();
		if(list == null || list.isEmpty()) {
			return new ResultVo(ResultEnmu.DATA_NULL);
		}
		return new ResultVo(ResultEnmu.SUCCESS,list);
	}

	// 冻结用户
	@GetMapping("/closeuser")
	public ResultVo closeuser(@RequestParam Integer uid) {
		int result = memberInfoService.closeuser(uid);
		if(result < 0) {
			return new ResultVo(ResultEnmu.ERROR);
		}
		return new ResultVo(ResultEnmu.SUCCESS);
	}

	// 解封用户
	@GetMapping("/openuser")
	public ResultVo openuser(@RequestParam Integer uid) {
		int result = memberInfoService.openuser(uid);
		if(result < 0) {
			return new ResultVo(ResultEnmu.ERROR);
		}
		return new ResultVo(ResultEnmu.SUCCESS);
	}
}
